package com.guickcheck.test;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import junit.framework.Test;
import junit.framework.TestSuite;

public class GuickTestSuite extends TestSuite {
  public GuickTestSuite() {
  }

  public void addTestSuite(Object theTests) {
    Class<?> theClass = theTests.getClass();

    Class superClass = theClass;
    while (Test.class.isAssignableFrom(superClass)) {
      Method[] methods = superClass.getDeclaredMethods();
      for (int i = 0; i < methods.length; i++) {
        addTestMethod(methods[i], theTests);
      }
      superClass = superClass.getSuperclass();
    }
  }

  private void addTestMethod(Method method, Object theTestTarget) {
    if (isPublicGuickTestMethod(method)) {
      addTest(new GuickTestCase(theTestTarget, method));
    }
  }

  private boolean isPublicGuickTestMethod(Method m) {
    return isGuickTestMethod(m) && Modifier.isPublic(m.getModifiers());
  }

  private boolean isGuickTestMethod(Method m) {
    String name = m.getName();
    Class[] parameters = m.getParameterTypes();
    Class returnType = m.getReturnType();
    return parameters.length == 0 && name.startsWith("test")
        && returnType.equals(Void.TYPE)
        && m.isAnnotationPresent(GuickTest.class);
  }
}
